import {request} from '../../request/index.js';

wx - Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [],
    cateList: [],
    floorList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var reqTask = wx.request({
    //   url: 'https://api-hmugo-web.itheima.net/api/public/v1/home/swiperdata',
    //   success: (result)=>{
    //     this.setData({
    //       swiperList:result.data.message
    //     })
    //   },
    // });
    this.getSwiperList();
    this.getCateList();
    this.getFloorList();
  },

  onPullDownRefresh() {
    this.setData({
      swiperList: [],
      cateList: [],
      floorList: []
    });
    this.getSwiperList();
    this.getCateList();
    this.getFloorList();
   },

  getSwiperList() { 
    request({
      url: "/home/swiperdata"
    }).then(
      result => {
        result.forEach((v, i) => {result[i].navigator_url = v.navigator_url.replace('main', 'index')});
        this.setData({
          swiperList: result
        })
      }
    )
    wx.stopPullDownRefresh();
  },
  getCateList() { 
    request({
      url: "/home/catitems"
    }).then(
      result => {
        this.setData({
          cateList: result
        })
      }
    )
    wx.stopPullDownRefresh();
  },
  getFloorList() { 
    request({
      url: "/home/floordata"
    }).then(
      result => {
        for (let k = 0; k < result.length; k++) {
          result[k].product_list.forEach((v, i) => {
            result[k].product_list[i].navigator_url = v.navigator_url.replace('?', '/index?');
          })
        }
        this.setData({
          floorList: result
        })
      }
    )
    wx.stopPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})